package com.zhzh.sc.demo.juc.interview;


import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: dasouche
 * @date: 2021-03-11 16:41
 **/
public class MyContainer1<T> {

    final private LinkedList<T> lists = new LinkedList();

    final private int MAX = 10;

    private int count = 0;

    ReentrantLock lock = new ReentrantLock();
    Condition consumer = lock.newCondition();
    Condition producer = lock.newCondition();

    public synchronized void put(T t) {
        while (lists.size() == MAX) {
            try {
//                this.wait();
                producer.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        lists.add(t);
        ++count;
//        this.notifyAll();
        consumer.signalAll();
    }

    public synchronized T get() {
        T t = null;
        while (lists.size() == 0) {
            try {
//                this.wait();
                consumer.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        t = lists.removeFirst();
        count--;
//        this.notifyAll();
        producer.signalAll();
        return t;
    }

    public static void main(String[] args) {
        MyContainer1<String> c = new MyContainer1<>();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 5; j++) {
                    System.out.println(c.get());
                }
            }, "c" + i).start();
        }

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                for (int j = 0; j < 25; j++) {
                    c.put(Thread.currentThread().getName() + " " + j);
                }
            }, "p" + i).start();
        }
    }
}
